package org.cyz.tools.core.assembly;


import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextInputDialog;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.util.Optional;

public class Alert {
    //0 NONE,1 INFORMATION ,2 WARNING, 3 CONFIRMATION,4 ERROR

    public static int tip(String msg,int type){
        javafx.scene.control.Alert alert = null;
        switch (type){
            case 0:
                alert=new javafx.scene.control.Alert(javafx.scene.control.Alert.AlertType.NONE);
                alert.titleProperty().set("提示");
                break;
            case 1:
                alert=new javafx.scene.control.Alert(javafx.scene.control.Alert.AlertType.INFORMATION);
                alert.titleProperty().set("信息");
                break;
            case 2:
                alert=new javafx.scene.control.Alert(javafx.scene.control.Alert.AlertType.WARNING);
                alert.titleProperty().set("警告");
                break;
            case 3:
                alert=new javafx.scene.control.Alert(javafx.scene.control.Alert.AlertType.CONFIRMATION);
                alert.titleProperty().set("确认");
                break;
            case 4:
                alert=new javafx.scene.control.Alert(javafx.scene.control.Alert.AlertType.ERROR);
                alert.titleProperty().set("错误");
                break;
        }
        alert.initStyle(StageStyle.UTILITY);
        alert.initModality(Modality.APPLICATION_MODAL);
        alert.headerTextProperty().set(msg);
        Optional<ButtonType> result= alert.showAndWait();
        if(ButtonType.OK==result.get()){
            return 1;
        }
        if(ButtonType.APPLY==result.get()){
            return 2;
        }
        return 0;
    }


    /**
     * 弹出输入框
     * @return
     */
    public static String inputIP(){
        TextInputDialog dialog = new TextInputDialog();
        dialog.initStyle(StageStyle.UTILITY);
        dialog.initModality(Modality.APPLICATION_MODAL);
        dialog.setTitle("信息输入");
        dialog.setHeaderText("要连接的地址,输入格式例如: 127.0.0.1:6379 ");
        dialog.setContentText("IP:Port > ");
        Optional<String> result = dialog.showAndWait();
        if (result.isPresent()){
            return result.get();
        }
       return null;
    }


    /**
     * 输入提示框
     * @param title
     * @param headerText
     * @param tips
     * @param content
     * @return
     */
    public static String inputBox(String title,String headerText,String tips,String content){
        TextInputDialog dialog = new TextInputDialog(content);
        dialog.initStyle(StageStyle.UTILITY);
        dialog.initModality(Modality.WINDOW_MODAL);
        dialog.setTitle(title);
        dialog.setHeaderText(headerText);
        dialog.setContentText(tips);
        Optional<String> result = dialog.showAndWait();
        if (result.isPresent()){
            return result.get();
        }
        return null;
    }


    public static void showTimedDialog(long time, String message) {
        Stage popup = new Stage();
        popup.setAlwaysOnTop(true);
        popup.initModality(Modality.APPLICATION_MODAL);
        Button closeBtn = new Button("知道了");
        closeBtn.setOnAction(e -> {
            popup.close();
        });
        VBox root = new VBox();
        root.setPadding(new Insets(20));
        root.setAlignment(Pos.BASELINE_CENTER);
        root.setSpacing(20);
        root.getChildren().addAll(new Label(message), closeBtn);
        Scene scene = new Scene(root);
        popup.setScene(scene);
        popup.setTitle("提示信息");
        popup.show();

        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(time);
                if (popup.isShowing()) {
                    Platform.runLater(() -> popup.close());
                }
            } catch (Exception exp) {
                exp.printStackTrace();
            }
        });
        thread.setDaemon(true);
        thread.start();
    }



}
